package com.sxh.user.controller;

import com.sxh.user.Utils.JwtUtil;
import com.sxh.user.Utils.TokenUtil;
import com.sxh.user.model.PageUser;
import com.sxh.user.model.UserInfo;
import com.sxh.user.model.Users;
import com.sxh.user.result.Result;
import com.sxh.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    //注册
    @PostMapping("/user/register")
    public Result userRegister(@RequestBody Users user){
        //判断非法数据，用户名不能为空
        if(StringUtils.isEmpty(user.getUsername())){
            System.err.println("用户名不能为空");
            return Result.error("用户名不能为空");
        }
        userService.userRegister(user);
        return Result.success();
    }
    //登录
    @PostMapping("/user/login")
    public Result userLogin(@RequestParam String username, @RequestParam String password){
        if(StringUtils.isEmpty(username)){
            System.err.println("用户名不能为空");
            return Result.error("用户名不能为空");
        }
        if(StringUtils.isEmpty(password)){
            System.err.println("密码不能为空");
            return Result.error("密码不能为空");
        }
        String token = userService.userLogin(username,password);
        return Result.success(token);
    }
    //查询用户信息
    @GetMapping("/user/{id}")
    public Result<UserInfo> queryUser(@PathVariable Long id){
        if(id == null){
            System.err.println("查询id为空");
            return Result.error("查询id为空");
        }
        //获取当前用户id
        String token = TokenUtil.getToken();
        Long ownerId = jwtUtil.parseToken(token);
        UserInfo userInfo = userService.queryUser(id,ownerId);
        return Result.success(userInfo);
    }
    //分页查询用户列表
    @GetMapping("/user/users")
    public Result<PageUser> queryUserList(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize){
        //获取当前用户id
        Long ownerId = jwtUtil.parseToken(TokenUtil.getToken());
        PageUser usersList = userService.queryUserList(pageNum,pageSize,ownerId);
        return Result.success(usersList);
    }
    //修改用户信息
    @PutMapping("/user/")
    public Result updateUser(@RequestBody UserInfo userInfo){
        //获取当前用户id
        Long ownerId = jwtUtil.parseToken(TokenUtil.getToken());
        userService.updateUser(userInfo,ownerId);
        return Result.success();
    }
    //密码重置
    @PostMapping("/user/reset-password")
    public Result resetPassword(@RequestParam Long id){
        if(id == null){
            System.err.println("重置密码id为空");
            return Result.error("重置密码id为空");
        }
        //获取当前用户id
        Long ownerId = jwtUtil.parseToken(TokenUtil.getToken());
        userService.resetPassword(id,ownerId);
        return Result.success();
    }
}
